package com.awspaas.user.apps.shhtaerospaceindustrial.event;

import java.util.List;
import java.util.Map;

import com.actionsoft.bpms.bpmn.engine.core.delegate.ProcessExecutionContext;
import com.actionsoft.bpms.bpmn.engine.listener.ExecuteListener;
import com.actionsoft.bpms.bpmn.engine.listener.ExecuteListenerInterface;
import com.actionsoft.bpms.commons.database.ColumnMapRowMapper;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.exception.BPMNError;
import com.actionsoft.sdk.local.SDK;
import com.awspaas.user.apps.shhtaerospaceindustrial.sms.SmsUtil;
import com.awspaas.user.apps.shhtaerospaceindustrial.util.CoreUtil;

public class NotifyVistorFor149bw extends ExecuteListener implements ExecuteListenerInterface {

	@Override
	public void execute(ProcessExecutionContext ctx) throws Exception {

//		boolean flag_em_submit = SDK.getTaskAPI().isChoiceActionMenu(ctx.getTaskInstance(), "提交");

		boolean flag_em_comfirm = SDK.getTaskAPI().isChoiceActionMenu(ctx.getTaskInstance(), "同意");

		if (flag_em_comfirm) {
			String car_sms_str[] = { "carnos", "carno2", "carno3" };
			String doorno_sms_str[] = { "doorno", "doorno2", "doorno3" };
			String permitdates_sms_str[] = { "permitdates", "permitdate2", "permitdate3" };
			String sms_template[] = { "SMS_271375493" };
			int door = 3;
			boolean isOneday = true;
			boolean isDepartment = false;
			boolean haveCar = false;
			String processInstId = ctx.getProcessInstance().getId();
			String carinfo_str = "";
//			        List<Map<String, Object>> visitorinfo = DBSql.query("SELECT * FROM BO_EU_VISITOR_MANAGE_MX WHERE BINDID = ? ", new ColumnMapRowMapper(), new Object[] {processInstId});
			List<Map<String, Object>> employeeinfo = DBSql.query(
					"SELECT * FROM BO_EU_VIT_MANGE_TOOFN WHERE BINDID = ? ", new ColumnMapRowMapper(),
					new Object[] { processInstId });
//			        List<Map<String,Object>> carinfo = DBSql.query("SELECT * FROM BO_EU_VISITOR_MANAGE_CARMX WHERE BINDID = ? ", new ColumnMapRowMapper(), new Object[] {processInstId});

			String carnos = "";
			String indate = "";
			String outdate = "";
			String sms_code = null;
			if (employeeinfo == null || employeeinfo.isEmpty()) {
				throw new BPMNError("无接待人员信息");
			}

			Map<String, Object> employee = employeeinfo.get(0);
			String e_name = CoreUtil.objToStr(employee.get("TARGETMAN"));
			String e_unit = CoreUtil.objToStr(employee.get("TARGETUNIT"));
			e_unit = "上海航天设备制造总厂有限公司";
			String ISHAVECAR = CoreUtil.objToStr(employee.get("ISHAVECAR"));
			String ISOPENFDOOR = CoreUtil.objToStr(employee.get("ISOPENFDOOR"));

			if ("1".equals(ISHAVECAR)) {
				haveCar = true;
				int i = 0;

				String carno = CoreUtil.objToStr(employee.get("VISITORCARS"));
				String instr = CoreUtil.objToStr(employee.get("INPARTTIME"));

				String time_in = instr;
//				String doorno = "二号门";

				carinfo_str += "','" + car_sms_str[i] + "':'" + carno + "','" + permitdates_sms_str[i] + "':'" + time_in;

				sms_code = sms_template[i];

			}

			String e_department = CoreUtil.objToStr(employee.get("TARGETDEPT"));
			String date = CoreUtil.objToStr(employee.get("INPARTTIME"));
			// String date = e_date.substring(0,e_date.lastIndexOf(":"));
			String e_phone = CoreUtil.objToStr(employee.get("TARGETMANPHONE"));

			SmsUtil sms = new SmsUtil();
			System.out.println("用户节点批准：-----------");

			if (haveCar == false) {
				String v_name = CoreUtil.objToStr(employee.get("VISITORNAME"));
				String message = "{'VISITORNAME':'" + v_name + "','date':'" + date + "','unitname':'" + e_unit
						+ "','deptname':'" + e_department + "','psnname':'" + e_name + "','mobilephone':'" + e_phone
						+ "'}";
				System.out.println(message);
				String v_phone = CoreUtil.objToStr(employee.get("VISITORCELL"));
				SmsUtil.sendSms(v_phone, "SMS_271020454", message);// old templtet is SMS_227253695

			} else {

				if (("1").equals(ISOPENFDOOR)) {

					String v_name = CoreUtil.objToStr(employee.get("VISITORNAME"));
					String message = "{'VISITORNAME':'" + v_name + "','date':'" + date + "','unitname':'" + e_unit
							+ "','deptname':'" + e_department + "','psnname':'" + e_name + "','mobilephone':'" + e_phone
							+ carinfo_str + "'}";
					System.out.println(message);
					String v_phone = CoreUtil.objToStr(employee.get("VISITORCELL"));
					SmsUtil.sendSms(v_phone, sms_code, message);
				}
//								}
			}

			if (SDK.getTaskAPI().isChoiceActionMenu(ctx.getTaskInstance(), "终止")) {
				String message = "{'date':'" + date + "','unitname':'" + e_unit + "','psnname':'" + e_name
						+ "','mobilephone':'" + e_phone + "'}";
				String v_name = CoreUtil.objToStr(employee.get("VISITORNAME"));
				String v_phone = CoreUtil.objToStr(employee.get("VISITORCELL"));
				SmsUtil.sendSms(v_phone, "SMS_227260006", message);
			}

			return;
		}

	}
}
